package top.songxs.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import top.songxs.yygh.common.exp.DiyException;
import top.songxs.yygh.hosp.repository.DepartmentRepository;
import top.songxs.yygh.hosp.service.DepartmentService;
import top.songxs.yygh.hosp.util.AckUtil;
import top.songxs.yygh.model.hosp.Department;
import top.songxs.yygh.vo.hosp.DepartmentVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentRepository departmentRepository;

    @Autowired
    AckUtil ackUtil;
    @Override
    public void save(Map<String, String> map) {
        // 校验权限   // 校验签名
        ackUtil.AckSign(map);

        //转换对象
        String s = JSON.toJSONString(map);
        Department department = JSON.parseObject(s, Department.class);

        //判断科室是否存在
        Department departmentFromMG = departmentRepository.findByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
            if (departmentFromMG == null){
                //如果没有则为新增 如果存在则为修改
                department.setCreateTime(new Date());
                department.setUpdateTime(new Date());
                departmentRepository.save(department);
            }else {
                department.setId(departmentFromMG.getId());
                department.setCreateTime(departmentFromMG.getCreateTime());
                department.setUpdateTime(new Date());
                departmentRepository.save(department);
            }
    }

    @Override
    public Page<Department> showDepartments(Map<String, String> map) {
        ackUtil.AckSign(map);

        String pageNum = map.get("page");
        String pageSize = map.get("limit");
        Pageable pageable = PageRequest.of(Integer.parseInt(pageNum)-1,Integer.parseInt(pageSize), Sort.by(Sort.Direction.DESC,"createTime"));
        //把条件封装成一个查询对象
        Department department = new Department();
        department.setHoscode(map.get("hoscode"));

        //如果还需要模糊查询  则需要一个macher对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnoreCase(true).withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        Example<Department> example = Example.of(department,exampleMatcher);
        Page<Department> page = departmentRepository.findAll(example,pageable);
        return page;
    }

    @Override
    public void remove(Map<String, String> map) {

        ackUtil.AckSign(map);
        String hoscode = map.get("hoscode");
        String depcode = map.get("depcode");

        Department departmentFromMG = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (departmentFromMG!=null){
            departmentRepository.deleteById(departmentFromMG.getId());
        }else {
            throw  new DiyException(20001,"不存在");
        }
    }



    //根据医院编号查询所有科室列表
    @Override
    public List<DepartmentVo> getDepartmentList(String hoscode) {
        List<Department> departments = departmentRepository.findByHoscode(hoscode);
        Map<String, List<Department>> groups = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));
        List<DepartmentVo> bigDep = new ArrayList<>();
        groups.forEach((s, departmentList) -> {
            DepartmentVo vo = new DepartmentVo();
            vo.setDepcode(s);
            vo.setDepname(departmentList.get(1).getBigname());
            List<DepartmentVo> departmentVos = deptToDeptVo(departmentList);
            vo.setChildren(departmentVos);
            bigDep.add(vo);
        });
        return bigDep;
    }

    @Override
    public Object getDepName(String hoscode, String depcode) {
        Department dep = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        if (dep == null) {
            return dep.getDepname();
        }
        return null;

    }

    private List<DepartmentVo> deptToDeptVo(List<Department> departmentList) {
        List<DepartmentVo> lists = new ArrayList<>();
        for (Department department : departmentList) {
            DepartmentVo vo = new DepartmentVo();
            BeanUtils.copyProperties(department,vo);
            lists.add(vo);
        }
        return lists;


    }
}
